#include "Libray.h"
namespace Books{

Libray::Libray(void){}

bool Libray::find_book(const Book& b) const
{
	for (int i=0; i < books.size(); ++i)
		if (books[i]==b)
			return true;
		return false;
}

void Libray::add_book(const Book& b)
{
	if (!find_book(b))
		books.push_back(b);
	else 
		throw("Book " + b.get_ISBN().get_value() + " " + b.get_name() + " already persist");
}


bool Libray::find_reader(const Patron& p) const
{
	for (int i=0; i < readers.size(); ++i)
		if (readers[i]==p)
			return true;
		return false;
}

void Libray::add_reader(const Patron& p)
{
	if (!find_reader(p))
		readers.push_back(p);
	else 
		throw("reader " + p.get_name() + " already persist");
}


bool Libray::is_on_hand(const Book& b) const 
{
	for (int i=0; i < transactions.size();++i)
		if (transactions[i].book==b)
			return true;
	return false;
}

void Libray::give_book(const Book& b, const Patron& p)
{
	if (!find_reader(p))
		throw my_err::Error("Reader " + p.get_name() + " unregistred!");

	if (!find_book(b))
		throw my_err::Error("Book " + b.get_name() + " unregistred!");

	if (is_on_hand(b))
		throw my_err::Error("Book " + b.get_name() + "already on hand!");

	if (have_debt(p))
		throw my_err::Error("Reader " + p.get_name() + " have debt!");

	transactions.push_back(Transaction(b,p,Date(2012,Date::apr, 24)));
}


vector<Patron> Libray::debter_list() const 
{
	vector <Patron> temp;
	
	for (int i=0; i < readers.size(); ++i)
		if (have_debt(readers[i]))
			temp.push_back(readers[i]);
	
	return temp;
}
}